﻿Imports System.ComponentModel
Imports System.Data.Linq.SqlClient

Partial Public Class ucDepartmentLedger2
    Dim dbInventoryDepartment As ATSDataClassesDataContext = New ATSDataClassesDataContext
    Private Sub ucDepartmentLedger_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        loadTreeView()
        loadGridView()
    End Sub

    Sub loadTreeView(Optional ByVal deptName As String = "")
        tvInventory.Items.Clear()
        Dim inventoryDB = From main In dbInventoryDepartment.MainAssetsTables _
                          Group Join departmentDetails In dbInventoryDepartment.Departments _
                          On main.DeptCode Equals departmentDetails.DepartmentCode _
                          Into RightTableResultsdepartmentDetails = Group _
                          From departmentDetails In RightTableResultsdepartmentDetails.DefaultIfEmpty _
                          Group Join categoryDetails In dbInventoryDepartment.Categories _
                          On main.CategoryCode Equals categoryDetails.CategoryCode _
                          Into RightTableResultscategoryDetails = Group _
                          From categoryDetails In RightTableResultscategoryDetails.DefaultIfEmpty _
                          Group Join disposeDetails In dbInventoryDepartment.DisposedAssets _
                          On main.Assetno Equals disposeDetails.Assetno _
                          Into RightTableResults = Group _
                          From disposeDetails In RightTableResults.DefaultIfEmpty _
                          Where disposeDetails.Assetno Is Nothing And _
                          If(InStr(deptName.ToUpper, "unclassified".ToUpper), departmentDetails.DepartmentName = "", If(deptName = "", departmentDetails.DepartmentName = departmentDetails.DepartmentName, departmentDetails.DepartmentName.Contains(deptName))) _
                          Group By DepartmentName = departmentDetails.DepartmentName _
                          , AssetClass = categoryDetails.AssetClass _
                          , CategoryName = categoryDetails.CategoryName _
                          , DescriptiveName = main.DescriptiveName _
                          , AssetNo = main.Assetno _
                          Into nygroup = Group _
                          Order By DepartmentName, AssetClass, CategoryName, DescriptiveName, AssetNo _
                          Select DepartmentName, AssetClass, CategoryName, DescriptiveName, AssetNo


        '        labeltest.Content = inventoryDB.Count

        Dim nodesCount As Integer = 0
        Dim nodesDepartment() As String = Nothing
        Dim nodesAssetClass() As String = Nothing
        Dim nodesCategoryName() As String = Nothing
        Dim nodesAssetName() As String = Nothing
        Dim nodesAssetNo() As String = Nothing

        Try
            For Each itemINV In inventoryDB
                ReDim Preserve nodesDepartment(nodesCount) : nodesDepartment(nodesCount) = itemINV.DepartmentName
                ReDim Preserve nodesAssetClass(nodesCount) : nodesAssetClass(nodesCount) = itemINV.AssetClass
                ReDim Preserve nodesCategoryName(nodesCount) : nodesCategoryName(nodesCount) = itemINV.CategoryName
                ReDim Preserve nodesAssetName(nodesCount) : nodesAssetName(nodesCount) = itemINV.DescriptiveName
                ReDim Preserve nodesAssetNo(nodesCount) : nodesAssetNo(nodesCount) = itemINV.AssetNo
                nodesCount += 1
            Next

            Dim departmentString As String = ""
            Dim assetClassString As String = ""
            Dim categoryNameString As String = ""
            Dim departmentNode As New TreeViewItem
            Dim assetClassNode As New TreeViewItem
            Dim categoryNameNode As New TreeViewItem
            Dim assetNameNode As TreeViewItem


            Dim node As New TreeViewItem
            node.Header = "Department"
            node.Name = "Department"
            node.Tag = HelperClass.AssetTreeLevel.defaultLevel
            tvInventory.Items.Add(node)

            node.IsExpanded = True

            For i As Integer = 0 To nodesCount - 1
                If nodesDepartment(i) <> departmentString Or i = 0 Then
                    departmentNode = New TreeViewItem()
                    departmentNode.Header = nodesDepartment(i)
                    If departmentNode.Header Is Nothing Then
                        departmentNode.Header = "Department - unclassified"
                    ElseIf departmentNode.Header.ToString.Length = 0 Then
                        departmentNode.Header = "Department - unclassified"
                    End If
                    node.Items.Add(departmentNode)
                    departmentString = nodesDepartment(i)
                    departmentNode.Tag = HelperClass.AssetTreeLevel.mainLevel
                    assetClassString = ""
                    categoryNameString = ""
                End If

                If nodesAssetClass(i) <> assetClassString Then
                    assetClassNode = New TreeViewItem
                    assetClassNode.Header = nodesAssetClass(i)
                    If assetClassNode.Header Is Nothing Then
                        assetClassNode.Header = "Class - unclassified"
                    ElseIf assetClassNode.Header.ToString.Length = 0 Then
                        assetClassNode.Header = "Class - unclassified"
                    End If
                    departmentNode.Items.Add(assetClassNode)
                    assetClassString = nodesAssetClass(i)
                    assetClassNode.Tag = HelperClass.AssetTreeLevel.classLevel
                    categoryNameString = ""
                End If

                If nodesCategoryName(i) <> categoryNameString Then
                    categoryNameNode = New TreeViewItem
                    categoryNameNode.Header = nodesCategoryName(i)
                    If categoryNameNode.Header Is Nothing Then
                        categoryNameNode.Header = "Category - unclassified"
                    ElseIf categoryNameNode.Header.ToString.Length = 0 Then
                        categoryNameNode.Header = "Category - unclassified"
                    End If
                    assetClassNode.Items.Add(categoryNameNode)
                    categoryNameString = nodesCategoryName(i)
                    categoryNameNode.Tag = HelperClass.AssetTreeLevel.categoryLevel
                End If

                assetNameNode = New TreeViewItem
                assetNameNode.Header = nodesAssetNo(i)
                If assetNameNode.Header Is Nothing Then
                    assetNameNode.Header = "Asset Name - unclassified"
                ElseIf assetNameNode.Header.ToString.Length = 0 Then
                    assetNameNode.Header = "Asset Name - unclassified"
                End If
                categoryNameNode.Items.Add(assetNameNode)
                assetNameNode.Tag = HelperClass.AssetTreeLevel.assetLevel
                'assetNameNode = New TreeNode(nodesAssetName(i))
                ''categoryNode.Nodes.Add(itemNode)
                'categoryNameNode.Nodes.Add(nodesAssetNo(i), nodesAssetName(i))
            Next
            node.Items.SortDescriptions.Add(New SortDescription("Header", ListSortDirection.Ascending))

        Catch ex As Exception
            Dim warningString As String = "There is a problem with the data." & ControlChars.Lf & ControlChars.Lf & ex.ToString & ControlChars.Lf & ControlChars.Lf & _
            "Please send the error to Gencpros"
            System.Windows.MessageBox.Show(warningString, "Data Error", MessageBoxButton.OK, MessageBoxImage.Exclamation)
        End Try




    End Sub

    Private Sub tvInventory_SelectedItemChanged(ByVal sender As Object, ByVal e As System.Windows.RoutedPropertyChangedEventArgs(Of Object)) Handles tvInventory.SelectedItemChanged
        Dim tvselectedItem As String
        '        labeltest.Content = tvInventory.SelectedValue.header.ToString
        tvselectedItem = tvInventory.SelectedValue.header.ToString
        'If InStr(tvselectedItem.ToUpper, "unclassified".ToUpper) Then
        ' tvselectedItem = ""
        ' End If
        Select Case tvInventory.SelectedValue.tag
            Case HelperClass.AssetTreeLevel.mainLevel
                loadGridView(tvselectedItem)
            Case HelperClass.AssetTreeLevel.classLevel
                Dim selectedNode As New TreeViewItem
                selectedNode = tvInventory.SelectedItem
                Dim departmentNode = selectedNode.Parent
                loadGridView(DirectCast(departmentNode, TreeViewItem).Header, selectedNode.Header)
            Case HelperClass.AssetTreeLevel.categoryLevel
                Dim selectedNode As New TreeViewItem
                selectedNode = tvInventory.SelectedItem
                Dim classNode As TreeViewItem = selectedNode.Parent
                Dim departmentNode As TreeViewItem = classNode.Parent
                loadGridView(departmentNode.Header, classNode.Header, tvselectedItem)
                'loadGridView(DirectCast(parentDir, TreeViewItem).Header, tvInventory.SelectedValue.header.ToString)
            Case HelperClass.AssetTreeLevel.assetLevel
                loadGridView(, , , tvselectedItem)

            Case Else
                loadGridView()
        End Select

    End Sub

    Private Sub loadGridView(Optional ByVal deptName As String = "", Optional ByVal className As String = "", Optional ByVal categoryName As String = "", Optional ByVal assetNumber As String = "")
        Dim itemListDB = From main In dbInventoryDepartment.MainAssetsTables Group Join departmentDetails In dbInventoryDepartment.Departments _
                          On main.DeptCode Equals departmentDetails.DepartmentCode _
                          Into RightTableResultsdepartmentDetails = Group _
                          From departmentDetails In RightTableResultsdepartmentDetails.DefaultIfEmpty _
                          Group Join categoryDetails In dbInventoryDepartment.Categories _
                          On main.CategoryCode Equals categoryDetails.CategoryCode _
                          Into RightTableResultscategoryDetails = Group _
                          From categoryDetails In RightTableResultscategoryDetails.DefaultIfEmpty _
                          Group Join personDetails In dbInventoryDepartment.Persons _
                          On main.AssignToEmployeeCode Equals personDetails.EmployeeID _
                          Into RightTableResultspersonDetails = Group _
                          From personDetails In RightTableResultspersonDetails.DefaultIfEmpty _
                          Group Join locationDetails In dbInventoryDepartment.Locations _
                          On main.LocationCode Equals locationDetails.LocationCode _
                          Into RightTableResultslocationDetails = Group _
                          From locationDetails In RightTableResultslocationDetails.DefaultIfEmpty _
                          Group Join disposeDetails In dbInventoryDepartment.DisposedAssets _
                          On main.Assetno Equals disposeDetails.Assetno _
                          Into RightTableResults = Group _
                          From disposeDetails In RightTableResults.DefaultIfEmpty _
                          Where disposeDetails.Assetno Is Nothing And _
                          If(InStr(deptName.ToUpper, "unclassified".ToUpper), departmentDetails.DepartmentName = "", If(deptName = "", departmentDetails.DepartmentName = departmentDetails.DepartmentName, departmentDetails.DepartmentName = deptName)) And _
                          If(InStr(className.ToUpper, "unclassified".ToUpper), categoryDetails.AssetClass = "", If(className = "", categoryDetails.AssetClass = categoryDetails.AssetClass, categoryDetails.AssetClass = className)) And _
                          If(InStr(categoryName.ToUpper, "unclassified".ToUpper), categoryDetails.CategoryName = "", If(categoryName = "", categoryDetails.CategoryName = categoryDetails.CategoryName, categoryDetails.CategoryName = categoryName)) And _
                          If(InStr(assetNumber.ToUpper, "unclassified".ToUpper), main.Assetno = "", If(assetNumber = "", main.Assetno = main.Assetno, main.Assetno = assetNumber)) _
                          Select main, categoryDetails, departmentDetails, locationDetails, _
                          personName = Trim(personDetails.LastName & If(personDetails.LastName.Length > 0 And personDetails.FirstName.Length > 0, ", ", " ") & personDetails.FirstName.ToUpper), _
                          assetCost = If(main.AssetAmount > 0, main.AssetAmount, main.BaseCost + main.VAT), _
                          depreciationCost = If(Not main.DateAcquired Is Nothing, _
                                                If(categoryDetails.Years > 0, _
                                                   ((If(main.AssetAmount > 0, main.AssetAmount, main.BaseCost + main.VAT) / categoryDetails.Years) / 12) * SqlMethods.DateDiffMonth(CDate(main.DateAcquired), DateTime.Now), _
                                                   0), _
                                                0)


        myDatagrid.ItemsSource = itemListDB.ToList
        textboxAcquisitionCost.Text = 0
        textboxAccumulatedDepreciation.Text = 0
        textboxNetBookValue.Text = 0
        Dim item As Object = Nothing
        For Each item In itemListDB
            textboxAcquisitionCost.Text += item.assetCost
            textboxAccumulatedDepreciation.Text += item.depreciationCost
            'If Not item.main.DateAcquired Is Nothing Then
            '    If item.categoryDetails.Years > 0 And CDate(item.main.DateAcquired) > #12:00:00 AM# Then
            '        textboxAccumulatedDepreciation.Text += ((item.assetCost / item.categoryDetails.Years) / 12) * DateDiff(DateInterval.Month, CDate(item.main.DateAcquired), Now)
            '        'textboxNetBookValue.Text += itemCost - ((itemCost / item.categoryDetails.Years) / 12) * DateDiff(DateInterval.Month, CDate(item.main.DateAcquired), Now)
            '    End If
            'End If
        Next
        textboxAcquisitionCost.Text = FormatCurrency(textboxAcquisitionCost.Text, 2)
        textboxAccumulatedDepreciation.Text = FormatCurrency(textboxAccumulatedDepreciation.Text, 2)
        'textboxNetBookValue.Text = FormatNumber(textboxNetBookValue.Text, 2)
        textboxNetBookValue.Text = FormatCurrency(textboxAcquisitionCost.Text - textboxAccumulatedDepreciation.Text, 2)

        If itemListDB.Count = 1 Then
            ExpanderText.Text = itemListDB.Count & " record found"
        Else
            ExpanderText.Text = itemListDB.Count & " records found"
        End If
    End Sub

    Private Sub searchButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles searchButton.Click
        loadTreeView(searchBox.Text)
    End Sub

    Private Sub searchBox_TextChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles searchBox.TextChanged
        loadTreeView(searchBox.Text)
    End Sub
End Class
